<?php
/**
 *    Copyright 2010 Show Framework(www.showframework.cn)
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/**
 * 数据库驱动管理类
 * 
 * @package Show_Db
 * @name Show_Db_Handle
 * @author show
 * @since v0.6
 */
class Show_Db_Handle
{
    /**
     * getHandle存放的驱动
     * 
     * @var Show_Db_Handle_Interface
     */
    protected static $handle;
    
    /**
     * 数据库驱动池
     *
     * @var array
     */
    protected static $pool = array();
    
    /**
     * 获取数据库驱动的工厂方法，此方法直接使用名称为db的配置
     *
     * @param string $type
     * @return Show_Db_Handle_Interface
     */
    public static function getHandle($type)
    {
        $config = Show_App::config()->db; //此方法直接使用名称为db的配置
        
        switch ($type) {
            case 'mysqli':
                self::$handle instanceof Show_Db_Handle_Mysqli ? $handle = self::$handle : $handle = new Show_Db_Handle_Mysqli($config->host, $config->user, $config->pass, $config->name);
                break;
            case 'mysql':
                self::$handle instanceof Show_Db_Handle_Mysql ? $handle = self::$handle : $handle = new Show_Db_Handle_Mysql($config->host, $config->user, $config->pass, $config->name);
                break;
            default:
                self::$handle instanceof Show_Db_Handle_Mysqli ? $handle = self::$handle : $handle = new Show_Db_Handle_Mysqli($config->host, $config->user, $config->pass, $config->name);
                break;
        }
        
        return $handle;
    }
    
    /**
     * 创建一个数据库驱动并注册接受管理
     * 注意：虽然没有太大影响，但是不要把此函数的调用放在可能产生循环的代码段里，正确的方法是在一处create，其他地方get
     *
     * @param string $type
     * @param Show_Config_Interface $config
     * @param string $name
     * @return void
     */
    public static function create($type, $config, $name)
    {
        if (isset(self::$pool[$name])) {
            return;
        }
        
        switch ($type) {
            case 'mysqli':
                self::$pool[$name] = new Show_Db_Handle_Mysqli($config->host, $config->user, $config->pass, $config->name);
                break;
            case 'mysql':
                self::$pool[$name] = new Show_Db_Handle_Mysql($config->host, $config->user, $config->pass, $config->name);
                break;
            default:
                self::$pool[$name] = new Show_Db_Handle_Mysqli($config->host, $config->user, $config->pass, $config->name);
                break;
        }
    }
    
    /**
     * 取得一个通过create创建的数据库驱动
     *
     * @param string $name
     * @return Show_Db_Handle_Interface|null
     */
    public static function get($name)
    {
        return isset(self::$pool[$name]) ? self::$pool[$name] : null;
    }
    
    /**
     * 删除驱动池中的一个驱动
     *
     * @param string $name
     * @return void
     */
    public static function delete($name)
    {
        if (isset(self::$pool[$name])) {
            self::$pool[$name]->close();
        }
        unset(self::$pool[$name]);
    }
}
?>